/*
 * hal.h
 *
 *  Created on: 13.12.2012
 *      Author: rpa
 */

#ifndef HAL_H_
#define HAL_H_

#include <intrinsics.h>

/*
 *  This macro is taken from TIMAC *
 *
 *  This macro is for use by other macros to form a fully valid C statement.
 *  Without this, the if/else conditionals could show unexpected behavior.
 *
 *  For example, use...
 *    #define SET_REGS()  st( ioreg1 = 0; ioreg2 = 0; )
 *  instead of ...
 *    #define SET_REGS()  { ioreg1 = 0; ioreg2 = 0; }
 *  or
 *    #define  SET_REGS()    ioreg1 = 0; ioreg2 = 0;
 *  The last macro would not behave as expected in the if/else construct.
 *  The second to last macro will cause a compiler error in certain uses
 *  of if/else construct
 *
 *  It is not necessary, or recommended, to use this macro where there is
 *  already a valid C statement.  For example, the following is redundant...
 *    #define CALL_FUNC()   st(  func();  )
 *  This should simply be...
 *    #define CALL_FUNC()   func()
 *
 * (The while condition below evaluates false without generating a
 *  constant-controlling-loop type of warning on most compilers.)
 */
#define st(x)      do { x } while (__LINE__ == -1)

typedef unsigned short halIntState_t;
#define HAL_ENTER_CRITICAL_SECTION(x)   st( x = _get_interrupt_state();  _disable_interrupts(); )
#define HAL_EXIT_CRITICAL_SECTION(x)    st( _set_interrupt_state(x); )

#endif /* HAL_H_ */
